'''
计算投票，增加分数权重,奖励为0.2，贿赂为0.2,0.4,0.6的情况下
'''

import random

import time


def get_random(bribe,profit):
    i = random.randint(-1000000000000000000,1000000000000000000)%100
    if i <int(bribe*100):    #被贿赂
        return -1
    elif i >int((1-profit)*100): #举报
        return 1
    else:
        return 0

def judge_abnormal(weights,round):
    out_list=[]
    for i in range(len(weights)):
        if weights[i] <20:
            out_list.append(i)
    if len(out_list)>0:
        pass
        #print('第{}轮的异常结果为：{}'.format(round,out_list))


def Correction(bribes,profits):
    for i in range(len(bribes)):
        if bribes[i] + profits[i]>=1:
            sum = bribes[i] + profits[i]
            bribes[i] = bribes[i]/sum
            profits[i] = profits[i]/sum
    return bribes,profits

def get_vote(people,bribes,profits):
    a=[]     #投票结果
    for i in range(people):
        a.append(get_random(bribes[i],profits[i]))    #根据比例生成随机数
    return a



def vote(Rate, people, timess, weight,growth):
    f=open('改进前.csv','a')
    start_time = time.time()
    all_result = []
    line_result = []
    weights = [weight] * people     #每个人的初始权重
    bribes = [Rate[0]] * people     #贿赂
    profits = [Rate[1]] * people     #举报
    a=get_vote(people,bribes,profits)    #第一轮投票
    result = []    #要输出的所有未被贿赂的票的概率
    for i in range(len(a)):
        if a[i] == 1:  # 举报添加奖励因子和权重
            profits[i] = (profits[i]+growth/2)
            weights[i] = weights[i] + 5
        elif a[i] ==-1:     #贿赂减少权重
            bribes[i] = (bribes[i]+growth)
            if  weights[i] - 5>=0:
                weights[i] = weights[i] - 5
    bribes, profits=Correction(bribes,profits)

    good_score = 0
    middle_score = 0
    bad_score = 0
    for n in range(people):
        if a[n] ==1:   #举报
            good_score = good_score + weights[n]
        elif a[n]==0:
            middle_score = middle_score  + weights[n]
        else:
            bad_score = bad_score + weights[n]
    if a.count(0) == 0:
        Numerator = good_score / a.count(1)
    else:
        Numerator = good_score / a.count(1) + middle_score / a.count(0)
    denominator =Numerator + bad_score/a.count(-1)
    result.append((a.count(1) + a.count(0)) / (a.count(-1) + a.count(0) + a.count(1)))
    print('好的节点：{}'.format(a.count(1) + a.count(0)))
    print('被举报的坏的节点：{}'.format(a.count(-1)))
    line_result.append(a.count(1) + a.count(0))
    line_result.append(a.count(-1))
    all_result.append(line_result)
    line_result = []

    #之后投票
    for times in range(1,timess+1):
        a = get_vote(people, bribes, profits)  # 投票
        for i in range(len(a)):
            if a[i] == 1:  # 举报添加奖励因子和权重
                profits[i] = (profits[i] + growth)
                weights[i] = weights[i] + 5
                time.sleep(0.000000001)
            elif a[i] == -1:  # 贿赂减少权重
                bribes[i] = (bribes[i] + growth)
                if weights[i] - 5 >= 0:
                    weights[i] = weights[i] - 5
        bribes, profits = Correction(bribes, profits)
        judge_abnormal(weights,times)
        good_score = 0
        bad_score = 0

        for n in range(len(weights)):
            if a[n] == 1:  # 举报
                good_score = good_score + weights[n]
            elif a[n] == 0:
                middle_score = middle_score + weights[n]
            else:
                bad_score = bad_score + weights[n]
        '''
                if a.count(0) == 0:
            Numerator  =  good_score / a.count(1)
        else:
            Numerator = good_score / a.count(1) + middle_score / a.count(0)
        denominator = Numerator + bad_score / a.count(-1)
        '''
        result.append((a.count(1)+a.count(0))/(a.count(-1)+a.count(0)+a.count(1)))
        print('好的节点：{}'.format(a.count(1) + a.count(0)))
        print('被举报的坏的节点：{}'.format(a.count(-1)))
        line_result.append(a.count(1) + a.count(0))
        line_result.append(a.count(-1))
        all_result.append(line_result)
        line_result = []
    out_str = ''
    for line in all_result:
        str_line = ''
        for each in line:
            str_line = str_line + '{},'.format(each)
        out_str = out_str + str_line[:-1] + '\n'
    end_time = time.time()
    result = end_time - start_time
    print('运行了{}s'.format(round(result, 3)))
    out_str = out_str + '{}\n'.format(result)
    f.write(out_str + '\n')

    return result

def caculate(bribe,profit):
    if bribe + profit>=1:
        sum = bribe+profit
        return bribe/sum,profit/sum
    else:
        return bribe,profit


if __name__ == '__main__':
    rate = []        #此时为举报率
    weight = 50
    people = 1000
    times = 20
    profits = [0.2,0.4,0.5]
    bribes = [0.2,0.4,0.6]
    growth = 0.1



    for profit in profits:
        for bribe in bribes:


            rate1,rate2 = caculate(bribe,profit)
            rate.append(rate1)#举报概率
            rate.append(rate2)#依次加入贿赂
            print('奖励为{},贿赂为{}的情况下：'.format(profit,bribe))
            a = vote(rate, people, times, weight,growth)
            print('{}\n'.format(a))
            rate = []    #重新初始化


